package com.tianze.tzkf.function.leadRecord;

import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.constraint.Guideline;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.tianze.tzkf.R;
import com.tianze.tzkf.base.BaseActivity;
import com.tianze.tzkf.base.BaseFragment;
import com.tianze.tzkf.bean.OrderListBean;
import com.tianze.tzkf.bean.StatisticsBean;
import com.tianze.tzkf.function.home.HomeActivity;
import com.tianze.tzkf.function.login.CheckPwdActivity;
import com.tianze.tzkf.function.orderDetails.OrderDetailActivity;
import com.tianze.tzkf.net.RetrofitFactory;
import com.tianze.tzkf.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Zcyang on 2018/12/27.
 */
public class LeadRecordFragment extends BaseFragment {


    private static final String TAG = LeadRecordFragment.class.toString();
    int pay_type = 0;


    @BindView(R.id.tv_lead_record_get_money)
    TextView tvLeadRecordGetMoney;
    @BindView(R.id.guideline5)
    Guideline guideline5;
    @BindView(R.id.tv_lead_record_get_count)
    TextView tvLeadRecordGetCount;
    @BindView(R.id.guideline7)
    Guideline guideline7;
    @BindView(R.id.cv_lead_record_get_money)
    CardView cvLeadRecordGetMoney;
    @BindView(R.id.tv_lead_record_refund_money)
    TextView tvLeadRecordRefundMoney;
    @BindView(R.id.guideline6)
    Guideline guideline6;
    @BindView(R.id.tv_lead_record_refund_money_count)
    TextView tvLeadRecordRefundMoneyCount;
    @BindView(R.id.guideline8)
    Guideline guideline8;
    @BindView(R.id.cv_lead_record_refund_money)
    CardView cvLeadRecordRefundMoney;
    @BindView(R.id.iv_lead_record_pay_type)
    ImageView ivLeadRecordPayType;
    @BindView(R.id.ll_lead_record_pay_type)
    LinearLayout llLeadRecordPayType;
    @BindView(R.id.rv_lead_record)
    RecyclerView rvLeadRecord;
    @BindView(R.id.srl_order_list)
    SmartRefreshLayout srlOrderList;

    @BindView(R.id.rl_lead_record_pay_Layout)
    RelativeLayout rl_lead_record_pay_Layout;

    @BindView(R.id.rl_lead_record_payUnlimited)
    RelativeLayout rl_lead_record_payUnlimited; // 不限
    @BindView(R.id.tv_lead_record_payUnlimited)
    TextView tv_lead_record_payUnlimited; // 不限

    @BindView(R.id.rl_lead_record_payAlipay)
    RelativeLayout rl_lead_record_payAlipay; // 支付宝
    @BindView(R.id.tv_lead_record_payAlipay)
    TextView tv_lead_record_payAlipay; // 支付宝

    @BindView(R.id.rl_lead_record_payWX)
    RelativeLayout rl_lead_record_payWX; // 微信
    @BindView(R.id.tv_lead_record_payWX)
    TextView tv_lead_record_payWX; // 微信

    @BindView(R.id.rl_lead_record_payBank)
    RelativeLayout rl_lead_record_payBank; // 银联
    @BindView(R.id.tv_lead_record_payBank)
    TextView tv_lead_record_payBank; // 银联


    private int page = 1;
    private LeadRecordPayAdapter mLeadRecordPayAdapter;

    public static LeadRecordFragment newInstance() {


        Bundle args = new Bundle();

        LeadRecordFragment fragment = new LeadRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }


    private void getOrderList(String page) {

        String token = ((BaseActivity) getActivity()).readToken().getToken();

        Map<String, String> request = new HashMap<>();
        request.put("token", token);
        request.put("pay_type", String.valueOf(pay_type));
        request.put("offset", "20");
        request.put("page", page);

        Observable<OrderListBean> login = RetrofitFactory.getInstance(TAG).getOrderList(request);
        login.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(OrderListBean orderListBean) {
                        if (orderListBean.getCode() == 10001) {
                            ToastUtils.showToast(getActivity(), orderListBean.getMsg());
                            List<OrderListBean.DataBean.ListBean> list = orderListBean.getData().get(0).getList();
                            if (TextUtils.equals("1", page)) {
                                bindList(list);
                            } else {
                                if (list.size() == 0 || list == null) {

                                    ToastUtils.showToast(getActivity(), "已无更多数据");
                                }
                                mLeadRecordPayAdapter.addData(list);
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        srlOrderList.finishLoadMore();
        srlOrderList.finishRefresh();


    }

    private void getStatistics() {

        String token = ((BaseActivity) getActivity()).readToken().getToken();

        Map<String, String> request = new HashMap<>();
        request.put("token", token);

        Observable<StatisticsBean> login = RetrofitFactory.getInstance(TAG).getStatistics(request);
        login.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<StatisticsBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(StatisticsBean statisticsBean) {
                        if (statisticsBean.getCode() == 10001) {
                            StatisticsBean.DataBean dataBean = statisticsBean.getData().get(0);
                            tvLeadRecordGetMoney.setText("+ " + dataBean.getPaySum());
                            tvLeadRecordRefundMoney.setText("- " + dataBean.getRefundSum());

                            String pay = "收款" + dataBean.getPayCount() + "笔";
                            String refund = "退款" + dataBean.getRefundCount() + "笔";

                            RelativeSizeSpan sizeSpan = new RelativeSizeSpan(2.0f);

                            SpannableString rateSpannableString = new SpannableString(pay);
                            rateSpannableString.setSpan(sizeSpan, 2, pay.length() - 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                            tvLeadRecordGetCount.setText(rateSpannableString);

                            SpannableString dateSpannableString = new SpannableString(refund);
                            dateSpannableString.setSpan(sizeSpan, 2, refund.length() - 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                            tvLeadRecordRefundMoneyCount.setText(dateSpannableString);

                        }
                        ToastUtils.showToast(getActivity(), statisticsBean.getMsg());

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    private void bindList(List<OrderListBean.DataBean.ListBean> data) {
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mLinearLayoutManager.setAutoMeasureEnabled(true);
        rvLeadRecord.setLayoutManager(mLinearLayoutManager);
        rvLeadRecord.setHasFixedSize(true);
        rvLeadRecord.setNestedScrollingEnabled(false);
        rvLeadRecord.setFocusable(true);

        List<String> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {

            list.add("123");
        }

        mLeadRecordPayAdapter = new LeadRecordPayAdapter(R.layout.item_lead_record_pay_type, data);
        rvLeadRecord.setAdapter(mLeadRecordPayAdapter);
        mLeadRecordPayAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ToastUtils.showToast(getActivity(), "点击了" + position);
//                intentDetail();
            }
        });


    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_lead_record;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initData() {
        pay_type = 0;
        initSelect();
        getStatistics();
        getOrderList(String.valueOf(page));

        srlOrderList.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getStatistics();
                getOrderList(String.valueOf(page));
            }
        });

        srlOrderList.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getOrderList(String.valueOf(page));
            }
        });

    }


    boolean pay_typeTage = false;
    @OnClick({R.id.cv_lead_record_get_money, R.id.cv_lead_record_refund_money, R.id.ll_lead_record_pay_type,R.id.rl_lead_record_payUnlimited,R.id.rl_lead_record_payAlipay,R.id.rl_lead_record_payWX,R.id.rl_lead_record_payBank})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.cv_lead_record_get_money:
                break;
            case R.id.cv_lead_record_refund_money:
                break;
            case R.id.ll_lead_record_pay_type: // 筛选

                if (pay_typeTage == false){
                    pay_typeTage = true;
                    rl_lead_record_pay_Layout.setVisibility(View.VISIBLE);
                }else {
                    pay_typeTage = false;
                    rl_lead_record_pay_Layout.setVisibility(View.GONE);
                }
                break;

            case R.id.rl_lead_record_payUnlimited: // 不限
                pay_type = 0;
                initSelect();
                getOrderList(String.valueOf(page));

                break;

            case R.id.rl_lead_record_payAlipay: // 支付宝
                pay_type = 1;
                initSelect();
                getOrderList(String.valueOf(page));
                break;

            case R.id.rl_lead_record_payWX: // 微信
                pay_type = 2;
                initSelect();
                getOrderList(String.valueOf(page));
                break;

            case R.id.rl_lead_record_payBank: // 银联
                pay_type = 3;
                initSelect();
                getOrderList(String.valueOf(page));
                break;
        }
    }



    public void initSelect(){

        Resources resource = (Resources) getActivity().getResources();
        ColorStateList cs_white = (ColorStateList) resource.getColorStateList(R.color.white);
        ColorStateList cs_black = (ColorStateList) resource.getColorStateList(R.color.black);

        switch (pay_type){
            case 0: // 不限
                rl_lead_record_payUnlimited.setBackgroundResource(R.drawable.box_home);
                tv_lead_record_payUnlimited.setTextColor(cs_white);

                rl_lead_record_payAlipay.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payAlipay.setTextColor(cs_black);
                rl_lead_record_payWX.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payWX.setTextColor(cs_black);
                rl_lead_record_payBank.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payBank.setTextColor(cs_black);
                break;
            case 1: // 支付宝
                rl_lead_record_payAlipay.setBackgroundResource(R.drawable.box_home);
                tv_lead_record_payAlipay.setTextColor(cs_white);

                rl_lead_record_payUnlimited.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payUnlimited.setTextColor(cs_black);
                rl_lead_record_payWX.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payWX.setTextColor(cs_black);
                rl_lead_record_payBank.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payBank.setTextColor(cs_black);
                break;
            case 2: // 微信
                rl_lead_record_payWX.setBackgroundResource(R.drawable.box_home);
                tv_lead_record_payWX.setTextColor(cs_white);

                rl_lead_record_payAlipay.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payAlipay.setTextColor(cs_black);
                rl_lead_record_payUnlimited.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payUnlimited.setTextColor(cs_black);
                rl_lead_record_payBank.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payBank.setTextColor(cs_black);
                break;
            case 3: // 银联
                rl_lead_record_payBank.setBackgroundResource(R.drawable.box_home);
                tv_lead_record_payBank.setTextColor(cs_white);

                rl_lead_record_payAlipay.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payAlipay.setTextColor(cs_black);
                rl_lead_record_payWX.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payWX.setTextColor(cs_black);
                rl_lead_record_payUnlimited.setBackgroundResource(R.drawable.box_home_light);
                tv_lead_record_payUnlimited.setTextColor(cs_black);
                break;
        }

    }


    public void intentDetail(){
        HomeActivity homeActivity = (HomeActivity) getActivity();
        Bundle bundle = new Bundle();
        homeActivity.close_WebSocket();
        homeActivity.close_Icard();
        homeActivity.goToActivity(OrderDetailActivity.class, bundle);
    }

}
